

from backend.cews.services.UploadService import import_stock_item_list, import_parts_master
from rest_framework.decorators import action
from rest_framework.viewsets import GenericViewSet
from rest_framework.response import Response
from utils.decorators import error_response

class UploadViewSet(GenericViewSet):
    @error_response(ValueError)
    @action(methods=['post'],detail=False,url_name='import_stock_item_master',url_path='import_stock_item_master')
    def import_stock_item_master(self,request):
        if request.FILES:
            file = request.FILES['file']
            ok = import_stock_item_list(file, request.user.username)
        
        if ok:
            return Response(status=200,
                        data={
                            'code': 1,
                        }) 
        
        else:
            return Response(status=200,
                        data={
                            'code': 0,
                        }) 
        
    @error_response()
    @action(methods=['post'],detail=False,url_name='import_parts_master',url_path='import_parts_master')
    def import_parts_master(self,request):
        if request.FILES:
            file = request.FILES['file']
            ok = import_parts_master(file, request.user.username)
        
        if ok:
            return Response(status=200,
                        data={
                            'code': 1,
                        }) 
        
        else:
            return Response(status=200,
                        data={
                            'code': 0,
                        }) 
 